var express = require('express');
var path = require('path');
var IO = require('socket.io');
var router = express.Router();
var db=require('./db');
var color=require('./color');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var app = express();
var server = require('http').Server(app);
app.use(express.static(path.join(__dirname, 'public')));
// app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.urlencoded());
// app.use(bodyParser).urlencoded({extended:false})
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('express-art-template'));
app.set('view engine', 'html');

// 创建socket服务
var socketIO = IO(server);
// 房间用户名单
var roomInfo = {public:[]};
// 建立连接
socketIO.on('connection', function (socket) {
  // 获取请求建立socket连接的url
  // 如: http://localhost:3000/room/room_1, roomID为room_1
  var url = socket.request.headers.referer;
  var url=url.split('?')
  var splited = url[0].split('/');
  var roomID = splited[splited.length - 1];   // 获取房间ID
  if(!roomID||roomID.trim()==''){
    roomID='public'
  }
  console.log(roomID)  
  var user = '';
  // 加入房间
  socket.on('join', function (userInfo) {    
    console.log(userInfo)
    var userName=userInfo.userName;
    roomID=userInfo.room;
    user = userName;
    // 将用户昵称加入房间名单中
    if (!roomInfo[roomID]) {
      roomInfo[roomID] = [];
    }
    roomInfo[roomID].push(user);

    socket.join(roomID);    // 加入房间
    // 通知房间内人员
    socketIO.to(roomID).emit('sys', user + '加入了聊天', roomInfo[roomID]);  
    console.log(user + '加入了' + roomID);
    
  });
  // 离开房间
  socket.on('leave', function () {
    socket.emit('disconnect');
  });
  // 掉线
  socket.on('disconnect', function () {
    // 从房间名单中移除
    var index = roomInfo[roomID].indexOf(user);
    if (index !== -1) {
      roomInfo[roomID].splice(index, 1);
    }

    socket.leave(roomID);    // 退出房间
    socketIO.to(roomID).emit('sys', user + '退出了聊天', roomInfo[roomID]);
    console.log(user + '退出了' + roomID);
  });
  // 接收用户消息,发送相应的房间
  socket.on('message', function (msg) {
    // 验证如果用户不在房间内则不给发送
    if (roomInfo[roomID].indexOf(user) === -1) {  
      return false;
    }
    socketIO.to(roomID).emit('msg', user, msg);
  });

});

// room page
// router.get('/room/:roomID', function (req, res) {
//   var roomID = req.params.roomID;
//   var user=req.query.user
//   // 渲染页面数据(见views/room.hbs)
//   res.render('room', {
//     roomID: roomID,
//     users: roomInfo[roomID],
//     user:user
//   });
// });

router.get('/users',(req,res)=>{
  res.json(roomInfo[roomID])
})
app.use('/', router);
app.use('/user', require('./routes/user'));
app.use('/topic', require('./routes/topic'));
app.use('/note', require('./routes/note'));
app.use('/badmin', require('./routes/BAdmin'));
app.use('/up_img', require('./routes/uploaderImg'));

server.listen(3001, function () {
  console.log('server listening on port 3001');
});
